<!-- file tracking -->
<script> 
	
	$(function(){
		/* external function */
		
		$.fn.serializeObject = function()
		{
		   var o = {};
		   var a = this.serializeArray();
		   $.each(a, function() {
			   if (o[this.name]) {
				   if (!o[this.name].push) {
					   o[this.name] = [o[this.name]];
				   }
				   o[this.name].push(this.value || '');
			   } else {
				   o[this.name] = this.value || '';
			   }
		   });
		   return o;
		};
		
		/* declarisasi */
		
		task_tracking = $("#task_tracking");
		grid_tracking = $("#grid-tracking");
		pgrid_tracking = $("#pgrid-tracking");
		btnadd_tracking = $("#btn-add-tracking");
		btnedit_tracking = $("#btn-edit-tracking");
		btndelete_tracking = $("#btn-delete-tracking");
		btncancel_tracking =  $("#btn-cancel-tracking");
		btnsave_tracking = $("#btn-save-tracking");
		form_tracking = $("#form-tracking");
		
		field_tracking_id = $("#tracking_id");
		field_m_flowdet_id = $("#m_flowdet_id");
		field_t_document_id = $("#t_document_id");
		field_tracking_date = $("#tracking_date");
		field_tracking_flag = $("#tracking_flag");
		
		/* method started */
		
		$( "input[type=submit],input[type=button],input[type=reset], button" ).button();
			
		/* make component */
		
		grid_tracking.jqGrid({ 
			ajaxGridOptions : {type:"POST"},
			serializeGridData : function(postdata) {return postdata; },
			postData : {task : 'LIST'},
            url: 'c_tracking',
			datatype: "json", 
			colNames:[
				'm_flowdet_id',
				't_document_id',
				'tracking_date',
				'tracking_flag',
				'tracking_id'],
			colModel:[
				{ name: 'm_flowdet_id'  ,index: 'm_flowdet_id' , search : true},
				{ name: 't_document_id'  ,index: 't_document_id' , search : true},
				{ name: 'tracking_date'  ,index: 'tracking_date' , search : true},
				{ name: 'tracking_flag'  ,index: 'tracking_flag' , search : true},
				{ name: 'tracking_id'  ,index: 'tracking_id',key: true, hidden: true , search : false}
			],
			width: '700',
			height: 'auto',
			rowNum:10, 
			rowList:[10,20,30], 
			pager: '#pgrid-tracking', 
			sortname: 'tracking_id', 
			viewrecords: true, 
			sortorder: "desc",
			caption:"" 
		}); 
		grid_tracking.jqGrid('navGrid','#pgrid-tracking',{edit:false,add:false,del:false,search:false});
		grid_tracking.jqGrid('filterToolbar',{});
		
		form_tracking.dialog({
			autoOpen: false,
			height: "auto",
			width: "auto",
			modal: true
		});
		
		/* event */

		btnedit_tracking.click(function(){
			id = grid_tracking.jqGrid("getGridParam","selrow");
			if (id != null){
				task_tracking.val("UPDATE");
				dataRow = grid_tracking.getRowData(id);
				field_tracking_id.val(dataRow.tracking_id);
				field_m_flowdet_id.val(dataRow.m_flowdet_id);
				field_t_document_id.val(dataRow.t_document_id);
				field_tracking_date.val(dataRow.tracking_date);
				field_tracking_flag.val(dataRow.tracking_flag);
				form_tracking.dialog("open");
			}
		});
		
		btndelete_tracking.click(function(){
			id = grid_tracking.jqGrid("getGridParam","selrow");
			if (id != null){
				confr = confirm("Apa Anda yakin menghapus data ini?");
				if (confr){
					$.ajax({
						type: 'POST',
						url: 'c_tracking',
						data: {tracking_id: id, task : 'DELETE'},
						success: function(response){
							if(response==1){
								grid_tracking.trigger('reloadGrid');
								$("#grid-tracking").trigger('reloadGrid');
							}
						}
					});
				}
			}
		});
		
		btnadd_tracking.click(function(){
			task_tracking.val("INSERT");
			form_tracking.dialog("open");
		});
		
		btncancel_tracking.click(function(){
			form_tracking.dialog("close");
		});
		
		btnsave_tracking.click(function(){
			data = form_tracking.serializeObject();
			$.ajax({
				url : "c_tracking",
				data : data,
				type : 'POST'
			}).done(function(response){
				grid_tracking.trigger('reloadGrid');
				btncancel_tracking.click();
			});
		});
		
	});
</script>

	<table id="grid-tracking" class="table table-striped table-bordered" ></table>
	<div id="pgrid-tracking"></div>
	
	<button id="btn-add-tracking">Tambah</button>
	<button id="btn-edit-tracking">Ubah</button>
	<button id="btn-delete-tracking">Hapus</button>
	
	<form id="form-tracking" title="tracking" disabled=true>
		<input type="hidden" value="INSERT" name="task" id="task_tracking">
	   <table>
			<tr style="display: none">
				<td align="right">tracking_id:</td>
				<td align="left"><input id="tracking_id" name="tracking_id" /></td>
			</tr>
			
			<tr > <td align="right">m_flowdet_id:</td> <td align="left"><input id="m_flowdet_id" name="m_flowdet_id" /></td> </tr>
			<tr > <td align="right">t_document_id:</td> <td align="left"><input id="t_document_id" name="t_document_id" /></td> </tr>
			<tr > <td align="right">tracking_date:</td> <td align="left"><input id="tracking_date" name="tracking_date" /></td> </tr>
			<tr > <td align="right">tracking_flag:</td> <td align="left"><input id="tracking_flag" name="tracking_flag" /></td> </tr>
			
			<tr>
				<td align="right"></td>
				<td style="padding-top: 10px;" align="right">
					<input style="margin-right: 5px;" type="button" id="btn-save-tracking" value="Simpan"  />
					<input id="btn-cancel-tracking" type="reset" value="Batal" /></td>
			</tr>
		</table>
	</form>